package com.xframework.security.dao;

import com.xframework.Base;
import com.xframework.model.SqlParamSeg;
import com.xframework.model.SqlParams;
import com.xframework.security.model.*;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by xuning on 2021/1/20.
 */
public interface SQL {

    static SqlParams getResourceByAccountApplicationSqlParams(String sql, SecurityAccount securityAccount) {
        SqlParams sqlParams = new SqlParams(
                sql,
                securityAccount,
                new SqlParamSeg(Base.BLANK, securityAccount.getApplicationId()),
                new SqlParamSeg("and t2.ACCOUNT_ID = ?", securityAccount.getAccountId()),
                new SqlParamSeg("order by SEQ")
        );
        return sqlParams;
    }

    static SqlParams getAccountSqlParams(String sql, Account account) {
        SqlParams sqlParams = new SqlParams(
                sql,
                account,
                new SqlParamSeg("and t1.ACCOUNT_ID = ?", account.getAccountId()),
                new SqlParamSeg("and (t1.ACCOUNT_NAME = ? or t1.ACCOUNT_MOBILE = ? or t1.ACCOUNT_EMAIL = ? or exists (select 1 from security_account_property t2 where t2.ACCOUNT_ID = t1.ACCOUNT_ID and t2.PROPERTY_VALUE = ?))", account.getAccountName(), account.getAccountName(), account.getAccountName(), account.getAccountName()),
                new SqlParamSeg("and t1.ACCOUNT_REMARK like concat('%', ?, '%')", account.getAccountRemark()),
                new SqlParamSeg("and t1.ACCOUNT_STATE = ?", account.getAccountState())
        );
        return sqlParams;
    }

    static SqlParams getRoleAccountSqlParams(String sql, AccountRole accountRole) {
        SqlParams sqlParams = new SqlParams(
                sql,
                accountRole,
                new SqlParamSeg("and t2.ROLE_ID = ?", accountRole.getRoleId())
        );
        return sqlParams;
    }

    static SqlParams getAccountPropertySqlParams(String sql, Property property) {
        SqlParams sqlParams = new SqlParams(
                sql,
                property,
                new SqlParamSeg("and t1.ACCOUNT_ID = ?", property.getAccountId()),
                new SqlParamSeg("and t1.PROPERTY_KEY = ?", property.getPropertyKey()),
                new SqlParamSeg("and t1.PROPERTY_VALUE = ?", property.getPropertyValue())
        );
        return sqlParams;
    }

    static SqlParams getApplicationSqlParams(String sql, Application application) {
        SqlParams sqlParams = new SqlParams(
                sql,
                application,
                new SqlParamSeg("and t1.APPLICATION_ID = ?", application.getApplicationId()),
                new SqlParamSeg("and t1.APPLICATION_NAME = ?", application.getApplicationName())
        );
        return sqlParams;
    }

    static SqlParams getResourceSqlParams(String sql, Resource resource) {
        SqlParams sqlParams = new SqlParams(
                sql,
                resource,
                new SqlParamSeg("and t1.APPLICATION_ID = ?", resource.getApplicationId()),
                new SqlParamSeg("and t1.RESOURCE_PARENT_ID = ?", resource.getResourceParentId()),
                new SqlParamSeg("and t1.RESOURCE_ID = ?", resource.getResourceId()),
                new SqlParamSeg("and t1.RESOURCE_STATE = ?", resource.getResourceState()),
                new SqlParamSeg("order by RESOURCE_SEQ")
        );
        return sqlParams;
    }

    static SqlParams getResourceTypeSqlParams(String sql, ResourceType resourceType) {
        SqlParams sqlParams = new SqlParams(
                sql,
                resourceType
        );
        return sqlParams;
    }

    static SqlParams getRoleSqlParams(String sql, Role role) {
        SqlParams sqlParams = new SqlParams(
                sql,
                role,
                new SqlParamSeg("and t1.ROLE_NAME = ?", role.getRoleName()),
                new SqlParamSeg("and t1.APPLICATION_ID = ?", role.getApplicationId())
        );
        return sqlParams;
    }

    static SqlParams getThirdRoleSqlParams(String sql, ThirdRole thirdRole) {
        SqlParams sqlParams = new SqlParams(
                sql,
                thirdRole,
                new SqlParamSeg("and exists(select 1 from SECURITY_THIRD_ROLE t2 where t1.ROLE_ID = t2.ROLE_ID and t2.THIRD_ID = ?)", thirdRole.getThirdId())
        );
        return sqlParams;
    }

    static SqlParams getRoleResourceSqlParams(String sql, RoleResource roleResource) {
        SqlParams sqlParams = new SqlParams(
                sql,
                roleResource,
                new SqlParamSeg("and t1.ROLE_ID = ?", roleResource.getRoleId())
        );
        return sqlParams;
    }

    final static RowMapper<String> roleResourceMapper = new RowMapper<String>() {
        @Override
        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
//                RoleResource roleResource = new RoleResource();
//                roleResource.setRoleId(rs.getInt("ROLE_ID"));
//                roleResource.setResourceId(rs.getInt("RESOURCE_ID"));
//                roleResource.setResourceSeq(rs.getInt("RESOURCE_SEQ"));
            return rs.getString("RESOURCE_ID");
        }
    };

}
